package com.fate.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.fate.framework.exception.KKException;
import com.fate.system.service.ITencentService;
import com.fate.system.vo.tencent.TencentImageVO;
import com.tencentcloudapi.bda.v20200324.BdaClient;
import com.tencentcloudapi.bda.v20200324.models.SegmentPortraitPicRequest;
import com.tencentcloudapi.bda.v20200324.models.SegmentPortraitPicResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ITencentServiceImpl implements ITencentService {

  @Value("${tencent.secretId}")
  private String secretId;

  @Value("${tencent.secretKey}")
  private String secretKey;

  @Override
  public TencentImageVO transferImage(String base64) {
    try {
      Credential cred = new Credential(secretId, secretKey);
      // 实例化一个http选项，可选的，没有特殊需求可以跳过
      HttpProfile httpProfile = new HttpProfile();
      httpProfile.setEndpoint("bda.tencentcloudapi.com");
      // 实例化一个client选项，可选的，没有特殊需求可以跳过
      ClientProfile clientProfile = new ClientProfile();
      clientProfile.setHttpProfile(httpProfile);
      // 实例化要请求产品的client对象,clientProfile是可选的
      BdaClient client = new BdaClient(cred, "ap-beijing", clientProfile);
      // 实例化一个请求对象,每个接口都会对应一个request对象
      SegmentPortraitPicRequest req = new SegmentPortraitPicRequest();
      req.setImage(base64);
      // 返回的resp是一个SegmentPortraitPicResponse的实例，与请求对象对应
      SegmentPortraitPicResponse resp = client.SegmentPortraitPic(req);
      // 输出json格式的字符串回包

      return JSON.parseObject(AbstractModel.toJsonString(resp), TencentImageVO.class);
    } catch (TencentCloudSDKException e) {
      throw new KKException(e.getMessage());
    }
  }
}
